
#include "hal_adc.h"

static ADC_Driver ADC0_driver;
static ADC_Driver ADC1_driver;

static ADC_Config usr_scan_config =
{
    ADC_OWREN,
    ADC_RIGHTALIGNED,
    ADC_MODE_SCAN,
    ADC_ADCLKSEL_0,
    ADC_ACKO,
    ADC_PWDN
};

static ADC_Config usr_oneshot_config =
{
    ADC_OWREN,
    ADC_RIGHTALIGNED,
    ADC_MODE_ONESHOT,
    ADC_ADCLKSEL_0,
    ADC_ACKO,
    ADC_PWDN
};

void hal_adc_init(adc_channel_en channel , adc_mode_en mode ,adc_bit_en bit , adc_trig_type_en trig_type,enabled_en en)
{
	static uint8_t init_flag0 = 1;
	static uint8_t init_flag1 = 1; 
    if((1 == init_flag0)&&(ADC_10BIT == bit))
    {    
    	ADC0_driver.index = ADC0_SELECT;
		if(ADC_SINGLE == mode)
		{
			ADC_LLD_Cfg_Init(&ADC0_driver, &usr_oneshot_config);
		}else if(ADC_CONTINU == mode)
		{
			ADC_LLD_Cfg_Init(&ADC0_driver, &usr_scan_config);
		}else
		{
			
		}
		init_flag0 = 0;
    }
    
    if((1 == init_flag1)&&(ADC_12BIT == bit))
    {    
    	ADC1_driver.index = ADC1_SELECT;
		if(ADC_SINGLE == mode)
		{
			ADC_LLD_Cfg_Init(&ADC1_driver, &usr_oneshot_config);
		}else if(ADC_CONTINU == mode)
		{
			ADC_LLD_Cfg_Init(&ADC1_driver, &usr_scan_config);
		}else
		{
			
		}
		init_flag1 = 0;
    }
    
    switch(trig_type)
    {
    case ADC_TRIG_TIM_1:
    	break;
    case ADC_TRIG_TIM_2:
    	break;
    case ADC_TRIG_PWM:
    	break;
    case ADC_TRIG_SW:
    	break;
    default:
    	break;
    }
    
    if(ADC_10BIT == bit)
    {
    	if(HAL_ENABLE == en)
    	{
    		ADC_LLD_ChannelConfig(&ADC0_driver, channel);
    	}
    }else if(ADC_12BIT == bit)
    {
    	if(HAL_ENABLE == en)
    	{
    		ADC_LLD_ChannelConfig(&ADC1_driver, channel);
    	}
    }else
    {
    	
    }
}

void hal_adc_start_convert(void)
{
    ADC_LLD_ConversionTiming(&ADC0_driver, ADC_CTR0_CH0_CH15, CTR_INPCMP_5, CTR_INPSAMP(60UL)); /* PRQA S 3469 */
    //ADC_LLD_ConversionTiming(&ADC1_driver, ADC_CTR0_CH0_CH15, CTR_INPCMP_5, CTR_INPSAMP(60UL)); /* PRQA S 3469 */
    ADC_LLD_ConversionTiming(&ADC1_driver, ADC_CTR1_CH32_CH59, CTR_INPCMP_5, CTR_INPSAMP(60UL)); /* PRQA S 3469 */
    ADC_LLD_ConversionTiming(&ADC1_driver, ADC_CTR2_CH64_CH95, CTR_INPCMP_5, CTR_INPSAMP(60UL)); /* PRQA S 3469 */
    ADC_LLD_Start(&ADC0_driver);
    ADC_LLD_Start(&ADC1_driver);
}

void hal_adc_stop_convert(void)
{
	ADC_Stop(&ADC0_driver);
	ADC_Stop(&ADC1_driver);
}

adc_status_en hal_adc_status(void)
{
	adc_status_en status = ADC_UNINIT;
	if((ADC_ECH_IntFlag(&ADC0_driver) == 1)&&(ADC_ECH_IntFlag(&ADC1_driver) == 1))
	{
		status = ADC_CONVERT_DONE;
	}
	else
	{
		status = ADC_CONVERTTING;
	}
	return status;
}

uint32_t hal_adc_result(adc_channel_en channel , adc_bit_en bit)
{
	uint32_t result = 0;
	switch(bit)
	{
	case ADC_10BIT:
		if(ADC_ECH_IntFlag(&ADC0_driver) == 1)
		{
			result = ADC_LLD_ChanValue(&ADC0_driver , channel);
		}
		else
		{
			result = 0;
		}
		break;
	case ADC_12BIT:
		if(ADC_ECH_IntFlag(&ADC1_driver) == 1)
		{
			result = ADC_LLD_ChanValue(&ADC1_driver , channel);
		}
		else
		{
			result = 0;
		}
		break;	
		break;
	default:
		result = 0;
		break;
	}
	return result;
}

